package com.maxmlr.music.admin.service.transfer;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.maxmlr.music.admin.entity.User;
import com.maxmlr.music.admin.entity.UserRole;
import com.maxmlr.music.admin.param.UserParam;
import com.maxmlr.music.common.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserTransfer
 * @Description TODO
 * @Author maxiao
 * @Date 2020/6/15 下午2:51
 * @Version V1.0
 */
@Component
public class UserTransfer {


    public User po2User(UserParam param, User adminUser) {
        Date date = DateUtil.getDate();
        User user = new User();
//        user.setId(param.getId());
        user.setUserName(param.getUserName());
        user.setName(param.getName());
        user.setPassword(MD5.create().digestHex(param.getPassword()));
        user.setEmail(param.getEmail());
        user.setMobile(param.getMobile());
        user.setStatus(param.getStatus());
//        user.setSex();
//        user.setBirth();
        user.setDeptId(param.getDeptId());
//        user.setPicId();
//        user.setLiveAddress();
//        user.setHobby();
//        user.setProvince();
//        user.setCity();
//        user.setDistrict();
        user.setCreateUserId(adminUser.getId());
        user.setCreateTime(date);
        user.setUpdateTime(date);
        return user;
    }

    public List<UserRole> po2UserRole(String roleIds, Integer userId) {
        String[] strings = StringUtils.split(roleIds, StrUtil.COMMA);
        return Arrays.stream(strings)
                .map(Integer::parseInt)
                .map(p -> {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(userId);
                    userRole.setRoleId(p);
                    return userRole;
                }).collect(Collectors.toList());
    }

    public UserParam po2UserParam(User user) {
        UserParam userParam = new UserParam();
        userParam.setId(user.getId());
        userParam.setUserName(user.getUserName());
        userParam.setName(user.getName());
        userParam.setPassword(user.getPassword());
        userParam.setEmail(user.getEmail());
        userParam.setMobile(user.getMobile());
        userParam.setStatus(user.getStatus());
        userParam.setDeptId(user.getDeptId());
        return userParam;
    }

    public void fullUser(User user, UserParam param, User adminUser) {
        Date date = DateUtil.getDate();
//        user.setUserName(param.getUserName());
        user.setName(param.getName());
//        user.setPassword(MD5.create().digestHex(param.getPassword()));
        user.setEmail(param.getEmail());
        user.setMobile(param.getMobile());
        user.setStatus(param.getStatus());
//        user.setSex();
//        user.setBirth();
        user.setDeptId(param.getDeptId());
//        user.setPicId();
//        user.setLiveAddress();
//        user.setHobby();
//        user.setProvince();
//        user.setCity();
//        user.setDistrict();
        user.setCreateUserId(adminUser.getId());
//        user.setCreateTime(date);
        user.setUpdateTime(date);
    }

    public void fullUserPwd(User user, UserParam param, User adminUser) {
        Date date = DateUtil.getDate();
        user.setPassword(MD5.create().digestHex(param.getPassword()));
        user.setCreateUserId(adminUser.getId());
        user.setUpdateTime(date);
    }
}
